<?php
	$loading_page = TRUE;
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	
	if (isset($_GET['building_id']) && isset($_GET['semester_id']) && isset($_POST['schedule']))
	{
		$building_id = mysql_real_escape_string(strip_tags($_GET['building_id']));
		$semester_id = mysql_real_escape_string(strip_tags($_GET['semester_id']));
		include('include/scheduler.php');
	}
	else
	{
		if (isset($_GET['building_id']))
		{
			$building_id = mysql_real_escape_string(strip_tags($_GET['building_id']));
		}
		else
		{
			$getBuilding = TRUE;
		}
		
		if (isset($_GET['semester_id']))
		{
			$semester_id = mysql_real_escape_string(strip_tags($_GET['semester_id']));
		}
		else
		{
			$getSemester = TRUE;
		}
	}

	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');
	?>
    <script type="text/javascript">
		$(document).ready(function(){
			$('.fade_out').hide();
			$('#schedule_button').click(function(){
				$('.fade_out').show();
			});
		});
	</script>

    <?php	
	if (isset($getSemester))
	{?>
    	<h1>Schedule Courses</h1>
    	<p><strong>Select the semester you wish to schedule:</strong>
        <ul>
    	<?php
		$query = "SELECT *, sem.id AS sem_id
				  FROM semesters sem
				  JOIN seasons ssn ON ssn.id = sem.season_id
				  ORDER BY year DESC, season_id DESC";
		$result = mysql_query($query);
		while($row = mysql_fetch_assoc($result))
		{?>
        	<li><a href="scheduleBuilding.php?semester_id=<?php echo $row['sem_id']; ?>"><?php echo $row['season'].' '.$row['year']; ?></a></li>
        <?php
		}
		?>
        </ul>
    <?php
	}
	else if (isset($getBuilding))
	{
		$query = "SELECT *, sem.id AS sem_id
				  FROM semesters sem
				  JOIN seasons ssn ON ssn.id = sem.season_id
				  WHERE sem.id = '$semester_id'";
		$result = mysql_query($query);
		$row = mysql_fetch_assoc($result);
		?>
        <p><a href="scheduleBuilding.php">&lt;&lt; Back to semesters</a></p>
        <h1>Schedule Courses</h1>
    	<p><strong>Select the building you wish to schedule for <?php echo $row['season'].' '.$row['year']; ?>:</strong>
        <ul>
    	<?php
		$query = "SELECT *
				  FROM buildings 
				  ORDER BY building_name";
		$result2 = mysql_query($query);
		while($row2 = mysql_fetch_assoc($result2))
		{?>
        	<li><a href="scheduleBuilding.php?semester_id=<?php echo $row['sem_id']; ?>&amp;building_id=<?php echo $row2['id']; ?>"><?php echo $row2['building_name']; ?></a></li>
        <?php
		}
		?>
        </ul>
    <?php	
	}
	else
	{

		$query = "SELECT *, sem.id AS sem_id
				  FROM semesters sem
				  JOIN seasons ssn ON ssn.id = sem.season_id
				  WHERE sem.id = '$semester_id'";
		$result = mysql_query($query);
		$row = mysql_fetch_assoc($result);
		$query = "SELECT *
				  FROM buildings 
				  WHERE id = '$building_id'";
		$result2 = mysql_query($query);
		$row2 = mysql_fetch_assoc($result2);
		?>
        
        <p><a href="scheduleBuilding.php?semester_id=<?php echo $semester_id; ?>">&lt;&lt; Back to buildings</a></p>
        <h1>Schedule Courses</h1>
        <p><strong>You have selected to schedule <?php echo $row2['building_name'].' for the '.$row['season'].' of '.$row['year']; ?></strong></p>
        <form name="schedule" method="post" action="scheduleBuilding.php?semester_id=<?php echo $semester_id; ?>&amp;building_id=<?php echo $building_id; ?>">
			<p><input type="submit" name="schedule" id="schedule_button" value="Click Here to Schedule Courses" />
        </form>
	<?php
	}
	?>
<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>